package com.darren.archive.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class FastjsonConfig {

    /**
     * 替换框架json为 FastJson返回
     *
     */

    @Bean
    public  HttpMessageConverters  fastJsonHttpMessageConverters(){
        //1.构建了一个HttpMessageConverterFastJson消息转换器
        FastJsonHttpMessageConverter  fastConverter=new  FastJsonHttpMessageConverter();
        //2.定义一个配置，设置编码方式，和格式化的形式
        FastJsonConfig   fastJsonConfig  =  new  FastJsonConfig();
        //3.设置PrettyFormat格式化，设置空字符，空集合返回空数组[]等
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,//是否格式化返回Json
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullStringAsEmpty);
        //4.处理中文乱码问题
        List<MediaType>  fastMediaTypes = new  ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        //设置输出JSON中的日期格式
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");

        //5.将fastJsonConfig加到消息转换器中
        fastConverter.setFastJsonConfig(fastJsonConfig);
        return  new   HttpMessageConverters(fastConverter);
    }


}
